﻿var game = $.connection.warriorsHub;

$(document).ready(function () {

/* General
--------------------------------*/
    $messages = $(".messages");
    $gamemessages = $("#main-chat");
    $log = $(".log");
    $gamebox = $(".game-box");
    $alert = $(".alerts");

    //Loading the game
    game.client.insertGame = function () {
        var content = "<div id=" + "sheet" + "> </div>" +
                      "<div id=" + "main-chat" + " ></div>" +
                      "<div id=" + "buttons" + ">" +
                      "<ul><li id=" + "attack" + ">Attack</li>" +
                      "<li id=" + "run" + ">Run</li></ul></div>" +
                      "<div id=" + "dice" + "><img src=" + "/Images/dice.png" +  " /></div>" +
                      "<textarea id=" + "message-box" + "></textarea>" +
                      "<input type=" + "button" + " value=" + "Send" + " id=" + "game-send" +  " />";

        $gamebox.append(content);
    };
    //Registering client
    game.client.register = function () {
        game.server.registerClient();
    };


    //Check if you can start the game
    game.client.testStart = function (started) {
        if (started) {
            game.server.loadGame();
            $("#StartGame").remove();
        }
        else {
            $(".alerts span").remove();
            $alert.append("<span>" + "You Need More Players to Start the Game" + "</span>");
        }
    };

/* Chat
--------------------------------*/
    
    //Add to Log
    game.client.connected = function (frm, list, date) {
        if ($("#" + frm).length <= 0) {

            for (i = 0; i < list.length; i++) {
                $("#" + list[i]).remove();
            }

            for (i = 0; i < list.length; i++) {
                $log.append("<span id=" + list[i] + ">" + list[i] + "</span>");
            }
        }
    };
    
    //Remove from Log
    game.client.disconnected = function (frm) {
        if ($("#" + frm).length > 0)
            $("#" + frm).remove();
    };
    
    //Add Message(Main)
    game.client.addMessage = function (frm, msg) {
        if (msg != "" || frm != "")
            $messages.append(frm + "  : " + msg + "</br>");
    };
    
    //Add Message(Game)
    game.client.addGameMessage = function (frm, msg) {
        if (msg != "" || frm != "")
            $("<span>" + frm + "  : " + msg + " </span></br>").appendTo("#main-chat");
    };

    //Add Message(Dungeon Master)
    game.client.addAdminMessage = function (frm, msg) {
        if (msg != "" || frm != "")
            $("<span class=" + "message-admin" + " >" + frm + "  : " + msg + " </span></br>").appendTo("#main-chat");
    };

    game.client.addGameAction = function (msg) {
        if (msg != "")
            $("<span class=" + "message-action" + " >"  + msg + " </span></br>").appendTo("#main-chat");
    };

    //Input Message(Main)
    $(".messagestext").keypress(function (e) {
        if (e.which == 13) {

            //get value of input
            var input = $(this).val();

            game.server.sendMessage(input);

            $(this).val("");

        }
    });

    //Input Message(Game)
    $("#game-send").live("click", function () {
        var input = $("#message-box").val();
        game.server.gameMessage(input);

        $("#message-box").val("");
    });


/* Lobby
-------------------------*/

    game.client.allThrow = function () {
        window.location = "/Home/Main";
    };

    $("#StartGame").click(function () {
        game.server.startGame();
    });

/* Game
-------------------------*/

//Roll dice
$("#dice").live("click", function () {
    game.server.dice();
});

//Display dice
game.client.showDice = function(frm, random) {
    if (frm != "")
        $("<span class=" + "diceroll" + ">" + frm + " has rolled " + random + "</span></br>").appendTo("#main-chat");;
};

//Load the sheet
game.client.loadSheet = function (HP, Str, Int, Dex, Class, Race, Name) {

    var sheet = "<div id=" + "top" + " >";
    if (Race == "Orc") 
        sheet += "<img class=" + "race-img" + " src=" + "/Images/orc.jpg" + " />";

    if (Race == "Human") 
        sheet += "<img class=" + "race-img" + " src=" + "/Images/human.jpg" + " />";

    sheet += "<span id=" + "charname" + " >" + Name + "</span>" + "</div>";
    sheet += "<div id=" + "skills" + " >" +
             "<span" + " class=" + "hp" + " >" + "Health Points: " + HP + " </span>" +
             "<span" + " class=" + "skill" + " >" + "Strength: "+ Str + " </span>" +
             "<span" + " class=" + "skill" + " >" + "Dexterity: "+ Dex + " </span>" +
             "<span" + " class=" + "skill" + " >" + "Intelligence: " + Int + "</span>" +
             "</div>";

             $(sheet).appendTo("#sheet");       
};

//Load the master Sheet
game.client.loadMasterSheet = function (Name) {

    var sheet = "<span id=" + "master-name" + "> You are the Dungeon master </span>" + 
        "<input type=" + "button" + " id=" + "kill" + " value=" + "Kill" + " /> " +
        "<input type=" + "text" + " id=" + "kill-val" + " />" +
        "<span> Commands: <input type="+ "text" + " id=" + "command-box" + " /> </span>"
    $(sheet).appendTo("#sheet");
};

//Kill
$("#kill").live("click", function () {
    var input = $("#kill-val").val();

    game.server.kill(input);
});

//Alert
game.client.alert = function (msg) {
    $(".alerts span").remove();
    $alert.append("<span>" + msg + "</span>");
};

//Alerting that the register is done
game.client.registerDone = function (Name) {
    $(".alerts span").remove();
    $alert.append("<span>" + Name + " has been added to this game" + "</span>");
};

game.client.callSheets = function () {
    game.server.addSheet();
};

    $("#command-box").live("keypress",function (e) {
    if (e.which == 13) {

        //get value of input
        var input = $(this).val();

        var n = input.split(":");
        var name = n[0];
        var HP = n[1];

        game.server.commandBox(name, HP);

        $(this).val("");

    }
});

game.client.editHP = function(newHp) {
    $(".hp").empty();
    $(".hp").text("HP: " + newHp);
};

$("#attack").live("click", function() {
    game.server.action("Attackes");
});

$("#run").live("click", function () {
    game.server.action("Runs");
});


$.connection.hub.start(function () {
   
});

});